package ebcs.scan.controller.batch;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import ebcs.database.bean.BsGroupInfor;
import ebcs.database.bean.BsProTeamInfor;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.PaParameter;
import ebcs.database.bean.RpPreDictionBean;
import ebcs.database.bean.RpUserReportBean;
import ebcs.database.bean.SysHoliday;
import ebcs.database.service.BsGroupInforServiceImp;
import ebcs.database.service.BsProTeamServiceImp;
import ebcs.database.service.BsUserInforServiceImp;
import ebcs.database.service.PaParameterServiceImp;
import ebcs.database.service.ReportServiceImp;
import ebcs.database.service.SystemServiceImp;
import ebcs.database.service.impl.BsUserInforServiceImpl;
import ebcs.scan.service.wechat.WeChatTemplPushService;
import ebcs.scan.service.wechat.bean.WechatTemplateSendData;
import ebcs.scan.service.wechat.bean.WechatTemplateSendDataDetail;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.config.JAppConfig;
import jplat.tools.string.DateUtil;
 

/***
 * 批处理
 * @author chenzhuo
 * @comment
 */
@Controller
@EnableScheduling
public class BatchManagerCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(BatchManagerCtrl.class);
 
 
	@Autowired
	private WeChatTemplPushService wechatPushService;
	@Autowired
	public BsUserInforServiceImp userInforService;
	@Autowired
	public SystemServiceImp systemService;
	@Autowired
	public BsGroupInforServiceImp bsGroupInfoServiceImp;
	@Autowired
	public ReportServiceImp reportService;
	@Autowired
	public PaParameterServiceImp paParameterService;
	@Autowired
	public BsProTeamServiceImp bsProTeamServiceImp;
	
	/**
	 * 手动触发通知
	 */ 
	@RequestMapping("/page/sys/batch/doHandleSendWechatMsg.do")
	public void doHandleSendWechatMsg( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);//自动客户信息
		//sendWechatMsgToGroupUser();
		//sendRegApplytoGroupUser("G5","G5T1","陈思丹");
		sendWechatMsgToGroupUser();
		Map rsModel = new HashMap();
		context.buildAppResponse(rsModel);
	}
	/**
	 * 发送微信通知(每半个小时触发一次)
	 */
	@Scheduled(cron="0 0 18 * * ?")
	public void sendWechatMsg(){
		String hour = DateUtil.getSystemTimeBySomeFormat("HH");
		logger.info("sendWechat hour:"+hour);
		List<SysHoliday> holidayList = systemService.selectHoliday();
		if(holidayList.size()>0) return ;
		if(Integer.parseInt(hour)>=18 && Integer.parseInt(hour)<= 20){
			List<BsUserInfor> userList = this.userInforService.selectUserInforByWechatUser();
			for(int i = 0 ;i<userList.size();i++){
				BsUserInfor user = userList.get(i);
				WechatTemplateSendData data = new WechatTemplateSendData();
				data.setFirst(new WechatTemplateSendDataDetail("您好，"+user.getUserName()+"，我是您的小助理！", "#173177"));
				data.setKeyword1(new WechatTemplateSendDataDetail("下班前，请记得打卡和报工哦！如果你已经操作过，请飘过......", "#000000"));
				data.setKeyword2(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyy年MM月dd日 hh:mm:ss"), "#000000"));
				data.setRemark(new WechatTemplateSendDataDetail("请点击详情进行处理", "#000000"));
				wechatPushService.doSendTemplateMsg(user.getOpenid(), JAppConfig.APP_URL + "/page/login/loginWechat.do", data,0);
			}
		}
	}
	
	
	/**
	 * 发送出勤率大于60的模版信息给各个团队经理
	 * 发送所有的出勤率大于60的给消息白名单的人
	 * 1.先查询所有团队经理的openid
	 * 发送微信通知(每一个小时触发一次)
	 */
	@Scheduled(cron="0 0 17 * * ?")
	public void sendWechatMsgToGroupUser(){
		String hour = DateUtil.getSystemTimeBySomeFormat("HH");
		logger.info("sendWechat hour:"+hour);
		List<SysHoliday> holidayList = systemService.selectHoliday();
		if(holidayList.size()>0) return ;
			List<BsGroupInfor> bsGroupInforList = bsGroupInfoServiceImp.selectGroupOpenid(null);
			for(int i = 0 ;i<bsGroupInforList.size();i++){
				BsGroupInfor bsGroupInfor = bsGroupInforList.get(i);
				
				//查询该团队经理今日的需求大于60%的需求个数
				RpUserReportBean record = new  RpUserReportBean();
				record.setProGroupNo(bsGroupInfor.getProGroupNo());
				List<RpPreDictionBean> rsList = this.reportService.selectCapReportLt60(record);
				if(rsList.size() <= 0) continue;
				//System.out.println("openid:"+bsGroupInfor.getOpenid() + ",name:"+bsGroupInfor.getGroupManger() );
				WechatTemplateSendData data = new WechatTemplateSendData();
				data.setFirst(new WechatTemplateSendDataDetail("您好，"+bsGroupInfor.getGroupManger()+"，我是您的小助理！", "#173177"));
				data.setKeyword1(new WechatTemplateSendDataDetail("今日团队的"+rsList.size()+"个需求评估大于60%的表格发送，请注意查收！如果已经查收就请忽略", "#000000"));
				data.setKeyword2(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyy年MM月dd日 hh:mm:ss"), "#000000"));
				data.setRemark(new WechatTemplateSendDataDetail("请点击详情进行处理", "#000000"));
				wechatPushService.doSendTemplateMsg(bsGroupInfor.getOpenid(), JAppConfig.APP_URL + "/page/main/reportLtSixty.do?proGroupNo="+bsGroupInfor.getProGroupNo(), data,0);
			}
			//查询所有白名单的人
			List<PaParameter> paParameterList = paParameterService.selectAllWhite();
			if(paParameterList.size() > 0){
					//查询所有团队的需求个数
					List<RpPreDictionBean> list = reportService.selectCapAllReportLt60();
					if(list.size() <=0) return;
					int size = list.size();
					for(int i = 0;i < paParameterList.size();i++){
					PaParameter paParameter = paParameterList.get(i);
					WechatTemplateSendData data = new WechatTemplateSendData();
					data.setFirst(new WechatTemplateSendDataDetail("您好，"+paParameter.getUserName()+"，我是您的小助理！", "#173177"));
					data.setKeyword1(new WechatTemplateSendDataDetail("今日团队的"+size+"个需求评估大于60%的表格发送，请注意查收！如果已经查收就请忽略", "#000000"));
					data.setKeyword2(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyy年MM月dd日 hh:mm:ss"), "#000000"));
					data.setRemark(new WechatTemplateSendDataDetail("请点击详情进行处理", "#000000"));
					wechatPushService.doSendTemplateMsg(paParameter.getOpenid(), JAppConfig.APP_URL + "/page/main/reportAllLtSixty.do", data,0);
				}
			}
	}
	
	
	public void sendWaitNeedToGroupUser(String dateStr,String groupManager,String openid,int size){
		WechatTemplateSendData data = new WechatTemplateSendData();
		data.setFirst(new WechatTemplateSendDataDetail("您好，"+groupManager+"，我是您的小助理！", "#173177"));
		data.setKeyword1(new WechatTemplateSendDataDetail("有"+size+"个上线需求等待评价", "#000000"));
		data.setKeyword2(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyy年MM月dd日 hh:mm:ss"), "#000000"));
		data.setRemark(new WechatTemplateSendDataDetail("请点击详情进行处理", "#000000"));
		wechatPushService.doSendTemplateMsg(openid, JAppConfig.APP_URL + "/page/main/findWaitCommentByDateStr.do?dateStr="+dateStr, data,0);
	}
	
	/**
	 * 发送业务审核给团队经理
	 * 1.根据groupNo查询出团队经理的openid和姓名
	 * 2.根据openid发送信息给团队经理
	 * @param groupNo 团队编号 用于查询团队经理的基本信息
	 * @param url 待处理业务链接
	 * @param proName 项目组名称
	 */
	public void sendRegApplytoGroupUser(String groupNo,String proTeamNo,String applyName){
		//List<SysHoliday> holidayList = systemService.selectHoliday();
		
		List<BsGroupInfor> bsGroupInforList = bsGroupInfoServiceImp.selectGroupOpenid(groupNo);
		//根据项目祖id获取项目组名称
		BsProTeamInfor proTeam = new BsProTeamInfor();
		proTeam.setProTeamNo(proTeamNo);
		List<BsProTeamInfor> bsProTeamInfors = bsProTeamServiceImp.selectProTeamDetail(proTeam);
		if(bsProTeamInfors != null && bsProTeamInfors.size() > 0)proTeam = bsProTeamInfors.get(0);
		if(bsGroupInforList != null && bsGroupInforList.size()>0){
			for(int i = 0;i < bsGroupInforList.size();i++){
				BsGroupInfor bsGroupInfor= bsGroupInforList.get(i);
				WechatTemplateSendData data = new WechatTemplateSendData();
				data.setFirst(new WechatTemplateSendDataDetail("您好，"+bsGroupInfor.getGroupManger()+"，您有一个待审批事项。", "#173177"));
				data.setKeyword1(new WechatTemplateSendDataDetail("【"+applyName+"】 发起【注册申请】", "#000000"));
				data.setKeyword2(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyyMMddHHmmss"), "#000000"));
				data.setKeyword3(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyy年MM月dd HH:mm:ss"), "#000000"));
				data.setKeyword4(new WechatTemplateSendDataDetail(applyName, "#000000"));
				data.setKeyword5(new WechatTemplateSendDataDetail("软件部-"+bsGroupInfor.getProGroupName()+"-"+proTeam.getProTeamName()+"", "#000000"));
				data.setRemark(new WechatTemplateSendDataDetail("请点击详情进行处理", "#000000"));
				wechatPushService.doSendTemplateMsg(bsGroupInfor.getOpenid(), JAppConfig.APP_URL + "/page/main/selectNoPassRegUserByRouid.do?rouid="+bsGroupInfor.getUserinfoRouid(), data,2);
			}
		}
	}
	
	/**
	 * 发送添加需求审核给团队经理审核
	 * @param applyName 申请人名称
	 * @param proTeamNo 项目组id
	 * @param proTeamName 项目组名称
	 */
	public void sendAddNeedApplyToGroupUser(String applyName,String proTeamNo){
		//根据项目组id查询出团队经理的信息
		List<BsGroupInfor> bsGroupInforList = bsGroupInfoServiceImp.selectGroupByProTeamNo(proTeamNo);
		for(int i = 0;i < bsGroupInforList.size();i++){
			BsGroupInfor bsGroupInfor= bsGroupInforList.get(i);
			WechatTemplateSendData data = new WechatTemplateSendData();
			data.setFirst(new WechatTemplateSendDataDetail("您好，"+bsGroupInfor.getGroupManger()+"，您有一个待审批事项。", "#173177"));
			data.setKeyword1(new WechatTemplateSendDataDetail("【"+applyName+"】 发起【添加需求申请】", "#000000"));
			data.setKeyword2(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyyMMddHHmmss"), "#000000"));
			data.setKeyword3(new WechatTemplateSendDataDetail(DateUtil.getSystemTimeBySomeFormat("yyyy年MM月dd HH:mm:ss"), "#000000"));
			data.setKeyword4(new WechatTemplateSendDataDetail(applyName, "#000000"));
			data.setKeyword5(new WechatTemplateSendDataDetail("软件部-"+bsGroupInfor.getProGroupName()+"-"+bsGroupInfor.getProTeamName()+"", "#000000"));
			data.setRemark(new WechatTemplateSendDataDetail("请点击详情进行处理", "#000000"));
			wechatPushService.doSendTemplateMsg(bsGroupInfor.getOpenid(), JAppConfig.APP_URL + "/page/need/selectNoPassNeedByRouid.do?rouid="+bsGroupInfor.getUserinfoRouid(), data,2);
		}
	}
}
